<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\shiyao;

use app\api\service\User as UserService;
use app\common\model\shiyao\ProjectTestee as ProjectTesteeModel;
use app\common\model\store\Setting as SettingModel;
use cores\exception\BaseException;
use think\facade\Db;

/**
 * 模型
 * Class
 * @package app\common\model
 */
class ProjectTestee extends ProjectTesteeModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'create_time',
		'update_time',
	];

	/**
	 * 获取收货地址列表
	 * @return \think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws BaseException
	 */
	public function getList(array $params = []) {
		$userId = UserService::getCurrentLoginUserId();
		$filter = [];
		$filter[] = ['center_id', '=', $params['centerId']];

		!empty($params['search']) && $filter[] = ['code|name', 'like', "%{$params['search']}%"];
		isset($params['is_queu']) && $filter[] = ['is_queu', '=', $params['is_queu']];

		// 实例化新查询对象
		$query = $this->getNewQuery();

		return $query->where($filter)
			->where('is_delete', '=', 0)
			->order('code asc')
			->select()->toArray();
	}

	/**
	 * 新增
	 * @param array $data
	 * @return mixed
	 * @throws BaseException
	 */
	public function add(array $data) {

		// 添加
		return $this->transaction(function () use ($data) {

			$password = SettingModel::getItem('trade', 10001)['order']['password'];

			//新增受试者账号
			$userInfo = Db::name('user')->where('mobile', $data['mobile'])->find();
			$testee = Db::name('shiyao_project_center_testee')->where('project_id', $data['project_id'])
				->where('center_id', $data['center_id'])->where('mobile', $data['mobile'])->where('idcard', $data['idcard'])->find();

			if ($testee) {
				throwError('请勿重复添加');
				return;
			}
			if ($userInfo) {
				$userId = $userInfo['user_id'];
				$userData['nick_name'] = $data['name'];
				$userData['is_delete'] = 0;
				Db::name('user')->where('user_id', $userId)->update($userData);
			} else {
				$userData['nick_name'] = $data['name'];
				$userData['mobile'] = $data['mobile'];
				$userData['password'] = $password;
				$userData['create_time'] = time();
				$userData['store_id'] = self::$storeId;
				$userId = Db::name('user')->insertGetId($userData);
			}
			if ($data['relation'] > 1) {

			} else {
				$data['bank_real_name'] = $data['name'];
			}

			$data['icf_date'] = strtotime($data['icf_date']);
			$data['user_id'] = $userId;

			$data = array_merge($data, [
				'store_id' => self::$storeId,
			]);
			$this->save($data);

			return true;
		});
	}

	/**
	 * 编辑
	 * @param array $data
	 * @return bool
	 * @throws BaseException
	 */
	public function edit(array $data) {
		if ($data['relation'] > 1) {

		} else {
			$data['bank_real_name'] = $data['name'];
		}
		$data['icf_date'] = strtotime($data['icf_date']);
		// 更新
		return $this->save($data) !== false;
	}

	/**
	 * 删除
	 * @return bool
	 * @throws BaseException
	 */
	public function remove() {

		// 标记为已删除
		return $this->delete();
	}

	/**
	 * 收货地址详情
	 * @param int $addressId
	 * @return UserAddress|array|null
	 * @throws BaseException
	 */
	public static function detail(int $testee_id) {

		$detail = self::get(['testee_id' => $testee_id], ['bank', 'idcardAvatar', 'idcardFile', 'certFile']);
		if (empty($detail)) {
			throwError('未找到该受试者');
		}
		return $detail;
	}
}
